import javax.swing.*;

import java.util.ListIterator;

import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;


import org.apache.*;
// org.w3c.dom.Node;
import org.apache.xerces.parsers.DOMParser;
//import org.apache.xerces.dom.traversal.NodeIteratorImpl;
//import org.apache.xerces.dom.DocumentImpl;
//import org.apache.xerces.domx.traversal.NodeFilter;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Component;
import java.awt.event.ItemListener;
import java.awt.event.ComponentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.Serializable;

public class RootMultistrokeMarkingMenu extends JPanel {
	
	public MultistrokeMarkingMenu rootMenu;
	
	ItemListener clistener;
	
	public RootMultistrokeMarkingMenu() {
		rootMenu = new MultistrokeMarkingMenu(null);
		
		//clistener = new ItemListener();
		experiment();
		setRootMenuPanelProperties();
	}
	
	public boolean setRootMenuPanelProperties() {
		//this.setLayout();
		this.setBackground(Color.WHITE);
		//this.setForeground(Color.BLACK);
		//this.setOpaque(true);
		this.setVisible(true);
		this.setSize(400, 400);
		//this.setLocation(333, 222);
		return true;
	}
	
	public void experiment() {
		JLabel label1 = new JLabel("Cool"); 
		label1.setLocation(200, 250);
		label1.setVisible(true);
		
		this.add(label1);
		
		//Document doc = this.parseXmlFile("MarkingSampleMenu.xml", false);
		//Document doc = this.parseXmlString(false);
		
		//System.out.print(doc);
		//this.saveToFile();
		
		
		//JFrame frame = new JFrame("Test Menu");
		//frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); 
	    //frame.getContentPane().add(this);
	    //int width = 1280;
	    //int height = 800;
	    //frame.setSize(width, height);
	    //frame.setVisible(true);
		
	}
	
	public MultistrokeMarkingMenu access(String tree) {
		
		// Parse a line whose separator is a comma followed by a space
		String patternStr = ", ";
	    String[] fields = tree.split(patternStr, -1);
	    
	    MultistrokeMarkingMenu tempMenu = rootMenu;
	    
	    for (int i = 1; i < fields.length; i++) {
	    	tempMenu = tempMenu.getMenuItemSubMenu(fields[i]);
	    }
	    return tempMenu;
	}
	
	
	public boolean popUpMenu(String tree) {
		MultistrokeMarkingMenu workingMenu = this.access(tree);
		ListIterator<MenuItem> iterator = workingMenu.getIterator();
		
		while (iterator.hasNext()) {
			
			MenuItem workingItem = iterator.next();
			//System.out.println(workingItem.getName());
			
			Graphics g = this.getGraphics();
			int x = 400;
			int y = 400;
			g.drawString(workingItem.getName(), x + (int)(workingItem.getCoordinates()[0]), y + (int)(workingItem.getCoordinates()[1]));
			
			
		}
		
		return true;
	}
	
	
	
	public void paint(Graphics g) {
		
		Dimension rootCoordinate = new Dimension(50, 50);
		
        // Dynamically calculate size information
        // (the canvas may have been resized externally...)
        Dimension size = getSize();
        int d = 200;//Math.min(size.width, size.height); // diameter
        int ed = 50;//d/20; // eye diameter
        
        
        int x = 300;//(size.width - d)/2;
        int y = 200;//(size.height - d)/2;
        
        
        // draw head (color already set to foreground)
        //g.fillOval(x, y, d, d);
        g.setColor(Color.black);
        //g.drawOval(x, y, d, d);

        // draw eyes
        
        
        //g.drawString("hello", (int)(rootCoordinate.getHeight()), (int)(rootCoordinate.getWidth()));
        //g.fillOval(x+d/3-(ed/2), y+d/3-(ed/2), ed, ed);
        //g.fillOval(x+(2*(d/3))-(ed/2), y+d/3-(ed/2), ed, ed);

        //draw mouth
        //g.drawArc(x+d/4, y+2*(d/5), d/2, d/3, 0, -180);
    }
	
	public MultistrokeMarkingMenu getRootMenu() {
		return rootMenu;
	}
	
	public void addRootMenuTo(Component c) {
		//c.addMouseListener(clistener);
		//c.addMouseMotionListener(clistener);
		//c.addComponentListener(clistener);
		//c.add(this);
	}
	
	public String toString() {
		String output = "<?xml version=\"1.0\"?>\n";
		output += "<MSMMs>\n";
		output += rootMenu;
		output += "</MSMMs>";
		return output;
	}
	
	
	public Document parseXmlString(boolean valid) {
        try {
        	
        	//DOMParser parser = new DOMParser();
        	//parser.parse(this.toString());
        	//Node root = parser.getDocument().getLastChild();
        	
        	
        	
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setValidating(valid);

            Document doc = factory.newDocumentBuilder().parse(new ByteArrayInputStream(this.toString().getBytes()));
            
            
            
            
            
            
            
            
            
            
            return doc;
        } catch (SAXException e) {
        	System.out.print(e.getMessage());
        } catch (ParserConfigurationException e) {
        	System.out.print(e.getMessage());
        } catch (IOException e) {
        	System.out.print(e.getMessage());
        }
        return null;
    }
    
    
    public  Document parseXmlFile(String filename, boolean validating) {
        try {
            DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
            fac.setValidating(validating);  
            Document doc = fac.newDocumentBuilder().parse(new File(filename));
            return doc;
        } catch (SAXException e) {
        	System.out.print(e.getMessage());
        } catch (ParserConfigurationException e) {
        	System.out.print(e.getMessage());
        } catch (IOException e) {
        	System.out.print(e.getMessage());
        }
        return null;
    }
    
    public boolean saveToFile() {
    	try {    
    		FileWriter writeSteam = new FileWriter("RootMenu.xml");
    		BufferedWriter out = new BufferedWriter(writeSteam);
    		out.write(this.toString());
    		out.close();
   		} catch (Exception e){
    		   System.err.println(e.getMessage());
    		   return false;
   		}
  		return true;
    }
	
	/*final class ItemListener
	implements MouseListener, MouseMotionListener, ComponentListener, Serializable {
		public final void mouseClicked(MouseEvent evt) {
			
		}
		
		public final void mouseExited(MouseEvent evt) {
		}
		
		public final void mouseEntered(MouseEvent evt) {
		}
		
		
		public final void mousePressed(MouseEvent evt) {
			
		}
		
		public final void mouseReleased(MouseEvent evt) {
			
		
		}
		
		public final void mouseDragged(MouseEvent evt) {
			
		}
		
		
		
		public final void mouseMoved(MouseEvent evt) {
			
		}
		
		public final void mouseWheelMoved(MouseEvent evt) {
			
		}
		
		public final void componentHidden(MouseEvent evt) {
			
		}
		public final void componentMoved(MouseEvent evt) {
			
		}
		
		public final void componentResized(MouseEvent evt) {
			
		}
		public final void componentShown(MouseEvent evt) {
			
		}
		//public final void componentHidden(MouseEvent evt) {
			
		//}
		
	}*/
	
	
	

}
